package com.fyzk.activity.share;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fyzk.base.BaseActivity;
import com.fyzk.data.preference.MySharedPreferences;
import com.fyzk.utils.DensityUtils;
import com.fyzk.utils.ImageUtils;
import com.fyzk.utils.ZXingUtils;
import com.fyzk.view.TopTitleView;
import com.tyjc.lottery.R;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.List;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

@ContentView(R.layout.activity_tuijian)
public class ShareTuijianActivity extends BaseActivity implements  EasyPermissions.PermissionCallbacks, EasyPermissions.RationaleCallbacks {
    @ViewInject(R.id.shareCodeUI_topView)
    private TopTitleView topView;
    @ViewInject(R.id.shareCodeUI_tvShareUrl)
    private TextView tvShareUrl;
    @ViewInject(R.id.shareCodeUI_ivShareCode)
    private ImageView ivShareCode;
    @ViewInject(R.id.shareCodeUI_ivShareCode_holder)
    private ImageView ivShareCode1;
    @ViewInject(R.id.holder_rl)
    private RelativeLayout mContent;
    private String shareUrl = "";
    private RelativeLayout content_rl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initEvent();
        initDate();
    }
    private void initEvent() {
        content_rl = findViewById(R.id.content_rl);
        topView.setCenterTexColor(Color.WHITE);
        topView.setLeftBtnImg(getDrawable(R.drawable.icon_back_white));
        topView.setRightTxtColor(Color.WHITE);
        topView.setOnTitleClickListener(this);
        content_rl.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                try {
                    if(hasPermissions()){
                        ImageUtils.saveImageToGallery(getContext(),createBitmap(mContent));
                    }else {
                        EasyPermissions.requestPermissions(ShareTuijianActivity.this, "正在请求使用您的手机权限，是否继续?", 101, STORAGE);
                    }
                }catch (Exception e){

                }
                return false;
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    private static final String[] STORAGE = {
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE};

    private boolean hasPermissions() {
        return EasyPermissions.hasPermissions(this, STORAGE);
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        try {
            ImageUtils.saveImageToGallery(getContext(),createBitmap(mContent));
        }catch (Exception e){}
    }

    /**
     * 权限申请失败
     *
     * @param requestCode
     * @param perms
     */
    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {//点击了不再提醒
            AppSettingsDialog.Builder builder = new AppSettingsDialog.Builder(this);
            builder.setTitle("权限申请");
            builder.setRationale("当前应用缺少必要权限。\n\n请在手机设置中开启所需权限，以便正常使用");
            AppSettingsDialog build = builder.build();
            build.show();
        } else {
            if (!EasyPermissions.hasPermissions(this, STORAGE)) {
                requestPermissions();
            }
        }
    }
    /**
     * 获取位置，手机唯一识别码与相机权限
     * 先判断是否有权限，有权限让使用app，没有权限，不让使用。
     */
    @AfterPermissionGranted(101)
    private void requestPermissions() {
        if (!hasPermissions()) {
            EasyPermissions.requestPermissions(this, "正在请求使用您的手机权限，是否继续?", 101, STORAGE);
        }
    }
    private void initDate() {
        shareUrl = MySharedPreferences.getInstance().getQrUrl() + "&agencyNo=" + MySharedPreferences.getInstance().getUserNo();
        tvShareUrl.setText(shareUrl);
        Bitmap bitmap = ZXingUtils.createBitmap(shareUrl, DensityUtils.dip2px(this, 220), DensityUtils.dip2px(this, 220));
        ivShareCode.setImageBitmap(bitmap);
        ivShareCode1.setImageBitmap(bitmap);
    }

    @Override
    public void onRightClick() {
        Intent intent = new Intent(this, MyInvitationActivity.class);
        startActivity(intent);
    }

    @Override
    protected int getStatusBarColor() {
        return R.color.color_FF4753;
    }

    private Bitmap createBitmap(View view) {
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        return bitmap;
    }

    @Override
    public void onRationaleAccepted(int requestCode) {

    }

    @Override
    public void onRationaleDenied(int requestCode) {

    }
}
